<?php
namespace Home\Controller;
use Model\DrawModel;
use Model\GameRoleModel;
use Think\Controller;
use \Model\UserModel;
class DrawController extends Controller {
    function room(){
        $uid=I("get.room_uid");
//        echo $uid;
        $users=DrawModel::get_room_users($uid);
        $this->assign("users",$users);
//        var_dump($users);
        $this->display();
    }
    function roomlist(){
        $rooms=DrawModel::get_room_list();
        $this->assign("rooms",$rooms);
        $this->display();
    }
    function inroom(){
        $uid=I("get.uid");
//        var_dump(DrawModel::is_playing($uid));
//        if(DrawModel::is_playing($uid)){
//            $tip=<<<eof
//            <script>alert("此房间已开始游戏！");
//            history.back();
//            </script>
//eof;
//        echo $tip;
//        }
//        DrawModel::inroom($uid,session("uid"));
//        //需要先验证登录状态，没登录则跳转至登录界面
//        $pc=new \Tools\PublicController();
//        $my=$pc->require_wx_login();
//        var_dump($users);
        DrawModel::inroom($uid,1);
        $users=DrawModel::get_room_users($uid);
        $room=D('room')->where("uid=".$uid)->find();
//        var_dump(explode(",",$room['has_ready_uids']));exit;
        $this->assign("users",$users);
        $this->assign("roomuid",$uid);
        $this->assign("has_ready",explode(",",$room['has_ready_uids']));
        $this->display();
    }
    function test(){
//        echo M("room")->getLastSql();//空模型
//        var_dump(DrawModel::get_room_list());
        var_dump(DrawModel::start(11));
    }
    function createroom(){
//        echo "d".session("uid");exit;
        //需要先验证登录状态，没登录则跳转至登录界面
        $pc=new \Tools\PublicController();
        $my=$pc->require_wx_login();
        $roomuid=DrawModel::create_room($my['uid']);
        if($roomuid){
            $this->redirect('Draw/inroom',array("uid"=>$roomuid),1," ");
        }
    }
    function wxcilentlogin(){
        if(!empty($_GET)) {//
//            var_dump($_GET);
            $res=self::weixin_https_get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx935cdb3a2a049e5c&secret=083e078b99a9b22e26d7d223ecd90c34&code=".$_GET['code']."&grant_type=authorization_code");
            $res=json_decode($res,true);
//            var_dump($res['access_token']);exit;
            $ress=self::weixin_https_get("https://api.weixin.qq.com/sns/userinfo?access_token={$res['access_token']}&openid={$res['openid']}&lang=zh_CN");
//            var_dump(json_decode($ress,true));
            $ress=json_decode($ress,true);
            if($ress['openid']){
                $user=\Model\DrawModel::adduser($ress);
//                var_dump($user);
                if($user&&!$user['status']){
                    $this->assign("user",$user);
                    $this->redirect("Draw/roomlist");
                }
            }
        }else{
//            header("Content-type: text/html; charset=utf-8");
            $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx935cdb3a2a049e5c&redirect_uri=".urlencode("http://192.168.1.7/index.php/Home/Draw/wxcilentlogin")."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
            $html = file_get_contents($url);
//            $res=self::weixin_https_get($url);
//            $res=json_encode($res);
            $str=<<<eof
            <!--<a href="$url">登陆</a>-->
            <script>window.location.href="$url"</script>
eof;
            echo $str;
//            var_dump($url);
        }

    }

    /*
		调用微信服务器接口 https get
	*/
    public static function weixin_https_get($url)
    {
        #Weixin::weixin_log('weixin get '.$url);
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
        $ret = curl_exec($c);
        curl_close($c);

        return $ret;
    }

    /*
        调用微信服务器接口 https post
    */
    public static function weixin_https_post($url, $data)
    {

        if (is_array($data))
        {
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        }
//        Weixin::weixin_log('weixin post ' . $url . ' ===== ' . $data);
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($c, CURLOPT_POST, true);
        curl_setopt($c, CURLOPT_POSTFIELDS, $data);
        $ret = curl_exec($c);
        curl_close($c);

        return $ret;
    }
}